<?php

namespace app\admin\controller;
use app\common\business\SysDept as SysDeptBus;
use app\common\lib\toCamelCase;
use app\admin\validate\SysDept as SysDeptValidate;
use app\common\business\DataAuth;

class SysDept extends AuthBase
{
    public function deptList(){
        try {
            $deptName = $this->request->param("deptName","","trim");
            $status = $this->request->param("status","");
            if($deptName){
                $where[] = ['dept_name', 'like', '%'.$deptName.'%'];
            }
            if ($status !== null && $status !== '') {
                // 如果$status不为null且不为空字符串，则添加条件到$where数组
                $where[] = ["status", "=", $status];
            }
            $where[] = ["del_flag", "=", 0];
            $where = (new DataAuth())->index($this->userId,$this->username,$this->dept_id,$where);
            $DeptData = (new SysDeptBus())->getNormalDept($where['where']);
            $DeptData = (new toCamelCase())->toCamelCaseArray($DeptData);
        }catch (\Exception $e){
            $DeptData = [];
        }
        return show(config("status.success"),"部门列表",$DeptData);
    }

    //部门添加
    public function deptAdd(){
        if (!$this->request->isPost()){
            return show(config("status.error"),"请求方式错误");
        }
        //参数校验
        $deptName = $this->request->param("deptName","","trim");
        $email = $this->request->param("email","","trim");
        $leader = $this->request->param("leader","","trim");
        $orderNum = $this->request->param("orderNum","","trim");
        $parentId = $this->request->param("parentId","","trim");
        $phone = $this->request->param("phone","","trim");
        $status = $this->request->param("status","","trim");
        $where[] = ['dept_id','=',$parentId];
        $deptData = (new SysDeptBus())->getFindNormalDept($where,'ancestors');
        $data = [
            'dept_name'=>$deptName,
            'email'=>$email,
            'leader'=>$leader,
            'order_num'=>$orderNum,
            'parent_id'=>$parentId,
            'phone'=>$phone,
            'status'=>$status,
            'ancestors'=>$deptData['ancestors'].",".$parentId,
        ];
        $validate = new SysDeptValidate();
        if(!$validate->check($data)){
            return show(config("status.error"),$validate->getError());
        }
        try {
            $result = (new SysDeptBus())->addDept($data);
        }catch (\Exception $e){
            return show(config('status.error'),$e->getMessage());
        }
        return show(config("status.success"),"新增成功",$result);
    }

    //部门修改
    public function deptEdit(){
        $allParams = request()->param();
        $data = (new toCamelCase)->toCamelCaseArray($allParams,1,2);
        try {
            $where[] = ['dept_id','=',$data['parent_id']];
            $deptData = (new SysDeptBus())->getFindNormalDept($where,'ancestors');
            if(empty($deptData)){
                $data['ancestors'] = $data['parent_id'];
            }else{
                $data['ancestors'] = $deptData['ancestors'].",".$data['parent_id'];
            }
            $result = (new SysDeptBus())->updateById($data['dept_id'],$data);
        }catch (\Exception $e){
            return show(config('status.error'),$e->getMessage());
        }
        return show(config("status.success"),"修改成功",$result);
    }

    //部门详情
    public function deptDetails(){
        $dept = $this->request->param("dept","","trim");
        if(empty($dept)){
            return show(config("status.error"),"参数不能为空");
        }
        try {
            $where[] = ["del_flag", "=", 0];
            $where[] = ["dept_id", "=", $dept];
            $DeptData = (new SysDeptBus())->getNormalDept($where);
            $DeptData = (new toCamelCase())->toCamelCaseArray($DeptData,2);
        }catch (\Exception $e){
            $DeptData = [];
        }
        return show(config("status.success"),"部门详情",$DeptData);
    }

    //删除部门
    public function deptDel(){
        try {
            $dept = $this->request->param("dept","","trim");
            if(empty($dept)){
                return show(config("status.error"),"参数不能为空");
            }
            $where[] = ["parent_id", "=", $dept];
            $DeptData = (new SysDeptBus())->getNormalDept($where);
            if($DeptData){
                return show(config("status.error"),"该部门下有下级部门，请先删除下级部门！");
            }
            $data = [
                'del_flag'=>2
            ];
            $result = (new SysDeptBus())->updateById($dept,$data);
        }catch (\Exception $e){
            return show(config('status.error'),$e->getMessage());
        }
        return show(config("status.success"),"删除成功",$result);
    }

    //排除节点
    public function deptExclude(){
        try {
            $dept = $this->request->param("exclude","","trim");
            if(empty($dept)){
                return show(config("status.error"),"参数不能为空");
            }
            $where[] = ["del_flag", "=", '0'];
            $where[] = ["dept_id", "<>", $dept];
            $where[] = ["parent_id", "<>", $dept];
            $DeptData = (new SysDeptBus())->getNormalDept($where);
            $DeptData = (new toCamelCase())->toCamelCaseArray($DeptData);
        }catch (\Exception $e){
            $DeptData = [];
        }
        return show(config("status.success"),"ok",$DeptData);
    }

    //部门无限极分类
    public function deptTree(){
        $where = (new DataAuth())->index($this->userId,$this->username,$this->dept_id);
        $DeptData = (new SysDeptBus())->deptTree($where['deptIdWhere']);
        $DeptData = array_merge($DeptData,$where['deptTree']);
        return show(config("status.success"),"ok",$DeptData);
    }
}
